package com.tiantian.quanminxiu.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jakewharton.rxbinding.view.RxView;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.model.User;
import com.tiantian.quanminxiu.utils.APPUtils;

import java.util.List;

/**
 * Created by admin on 2016/1/14.
 */
public class UserAvatarAdapter extends RecyclerView.Adapter<UserAvatarAdapter.MyViewHolder>
{

    private Activity activity;
    private List<User> list;
    public UserAvatarAdapter(Activity activity, List<User> list){
        this.activity = activity;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                activity).inflate(R.layout.useravataradapter_item, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position)
    {
        User user = list.get(position);
        holder.vHasGuard.setVisibility(View.GONE);
        if(user.isRoomGuardian()){
            holder.vHasGuard.setVisibility(View.VISIBLE);
        }
        holder.imgUserAvatar.post(new Runnable() {
            @Override
            public void run() {
                APPUtils.displayImage(APPUtils.createUserAvatar(user.getUser_ID()), holder.imgUserAvatar,activity.getBaseContext(), holder.imgUserAvatar.getHeight() / 2);
            }
        });
        holder.vContent.setTag(user);
        RxView.clicks(holder.vContent).map(View->holder.vContent).subscribe(v->{
            User u = (User) v.getTag();
            APPUtils.Dialog_UserInfoCard_To(activity,u.getUser_ID(),u.getNickName(),-1,u.getLevel(),-1,false);
        });
    }

    @Override
    public int getItemCount()
    {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {
        View vHasGuard,vContent;
        ImageView imgUserAvatar;

        public MyViewHolder(View view)
        {
            super(view);
            imgUserAvatar = (ImageView) view.findViewById(R.id.imgUserAvatar);
            vHasGuard = view.findViewById(R.id.vHasGuard);
            vContent = view.findViewById(R.id.vContent);
        }
    }

}
